<?php

class Phanphoi_m extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function getall($limit, $offset, $id = 0) {
        if (!is_numeric($id) || $id < 0)
            return null;

        if ($id == 0) {
            if ($limit == 0) {
                $this->db->order_by('id', 'DESC');
                $query = $this->db->get('phanphoi');
                return $query->result();
            } else {

                $this->db->order_by('id', 'DESC');
                $this->db->limit($limit, $offset);
                $query = $this->db->get('phanphoi');
                return $query->result();
            }
        } else {
            $this->db->where('id', $id);
            $query = $this->db->get('phanphoi');
            return $query->result();
        }
    }

    function get_news($id=0) {
        if (!is_numeric($id) || $id < 0)
            return null;

        if ($id == 0) {
            $this->db->order_by('id', 'DESC');
            $this->db->where('status',1);
            $query = $this->db->get('news');
            return $query->result();
        } else {
            $this->db->where('id', $id);
            $query = $this->db->get('news');
            if ($query->num_rows > 0)
                return $query->first_row();
        }
    }

   


    function add($data) {
     
        $data['time'] = time();
        if ($this->db->insert('phanphoi', $data))
            return $this->db->insert_id();
        else
            return false;
    }

    function update($id, $data) {
       
        $data['time'] = time();

        $this->db->where('id', $id);
        if ($this->db->update('phanphoi', $data))
            return true;
        else
            return false;
    }

    function delete($ids) {
        if (is_array($ids)) {
            foreach ($ids as $id) {
                $this->db->where('id', $id);
                $this->db->delete('phanphoi');
            }
            return true;
        }else {
            if (!is_numeric($ids))
                return false;
            $this->db->where('id', $ids);
            if ($this->db->delete('phanphoi'))
                return true;
            else
                return false;
        }
    }

 

}
